<script>
import { checkToken, refreshToken } from '@/api/token/token.js';
import { teacherInfo } from '@/api/teacher/teacherInfo/teacherInfo.js';

export default {
	onLaunch: async function() {
		// uni.setStorageSync('userToken', '9370c9a3-f846-4fe4-9775-7ee17ad1663a');
		getUserInfo();
	},
	onShow: function() {},
	onHide: function() {}
};

async function getUserInfo() {
	let checkLocal = window.location.href;
	// let inviteid = '';
	let local;
	let id = getUrlParam('id');
	if (id) {
		uni.setStorageSync('shareId', id);
		console.log(uni.getStorageSync('shareId'));
	}
	local = 'http://qianhe.westts.cn/index.html';

	if (checkLocal.indexOf('/pages/teacher/') == -1) {
		if (uni.getStorageSync('userToken')) {
			let res = await checkToken(uni.getStorageSync('userToken'));
			if (res.code != 1) {
				let appid = 'wxc5f62af1cae2b8d9';
				let code = getUrlParam('code'); //是否存在code
				//不存在就打开上面的地址进行授权
				if (code == null || code === '') {
					window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(
						local
					)}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
					// uni.request({
					// 	url: 'http://qianhe.westts.cn/api/user.user/third',
					// 	method: 'GET',
					// 	data: {
					// 		inviteid: inviteid,
					// 		code: code
					// 	},
					// 	success: res => {
					// 		uni.setStorageSync('userToken', res.data.userinfo.token);
					// 	},
					// 	fail: err => {
					// 		uni.showToast({
					// 			title: '登录失败，请退出重新登录',
					// 			icon: 'none',
					// 			duration: 5000
					// 		});
					// 	},
					// 	complete: () => {}
					// });
				}
			}
		} else {
			let appid = 'wxc5f62af1cae2b8d9';
			let code = getUrlParam('code'); //是否存在code
			if (code == null || code === '') {
				window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(
					local
				)}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
				// uni.request({
				// 	url: 'http://qianhe.westts.cn/api/user.user/third',
				// 	method: 'GET',
				// 	data: {
				// 		inviteid: inviteid,
				// 		code: code
				// 	},
				// 	success: res => {
				// 		uni.setStorageSync('userToken', res.data.userinfo.token);
				// 	},
				// 	fail: err => {
				// 		uni.showToast({
				// 			title: '登录失败，请退出重新登录',
				// 			icon: 'none',
				// 			duration: 5000
				// 		});
				// 	},
				// 	complete: () => {}
				// });
			}
			//不存在就打开上面的地址进行授权
		}
	} else {
		if (uni.getStorageSync('teacherToken')) {
			let get = await teacherInfo(uni.getStorageSync('teacherToken'));
			if (get.msg != 'success') {
				uni.reLaunch({
					url: '/pages/teacher/login'
				});
			}
		} else {
			uni.reLaunch({
				url: '/pages/teacher/login'
			});
		}
	}
}

// 判断公众号截取code
const getUrlParam = name => {
	let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
	let r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return unescape(r[2]);
	}
	return null;
};

// 判断是否为公众号模拟器环境
const isWechat = () => {
	return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === 'micromessenger';
};
</script>
<style lang="scss">
/*每个页面公共css */

.blueBot {
	width: 6rpx;
	height: 34rpx;
	background-color: #4b76f9;
	border-radius: 3rpx;
}

.borderBottom10 {
	border-bottom: 10rpx solid #f7f7f7;
}

.borderBottom1 {
	border-bottom: 1rpx solid #d7d7d7;
}

.borderBottom30 {
	border-bottom: 30rpx solid #f7f7f7;
}

.borderBottom20 {
	border-bottom: 20rpx solid #f7f7f7;
}

@font-face {
	font-family: 'iconfont'; /* project id 1711267 */
	src: url('//at.alicdn.com/t/font_1711267_ib0gn1x1m1p.eot');
	src: url('//at.alicdn.com/t/font_1711267_ib0gn1x1m1p.eot?#iefix') format('embedded-opentype'), url('//at.alicdn.com/t/font_1711267_ib0gn1x1m1p.woff2') format('woff2'),
		url('//at.alicdn.com/t/font_1711267_ib0gn1x1m1p.woff') format('woff'), url('//at.alicdn.com/t/font_1711267_ib0gn1x1m1p.ttf') format('truetype'),
		url('//at.alicdn.com/t/font_1711267_ib0gn1x1m1p.svg#iconfont') format('svg');
}

.iconfont {
	font-family: 'iconfont' !important;
	font-size: 28rpx;
	font-style: normal;
	-webkit-font-smoothing: antialiased;
	-webkit-text-stroke-width: 0.2px;
	-moz-osx-font-smoothing: grayscale;
}

.skillList {
	display: flex;
	flex-wrap: wrap;
	.characteristic {
		margin-top: 10rpx;
		padding: 0 2rpx;
		height: 32rpx;
		background-color: #e1edfe;
		font-size: 20rpx;
		text-align: center;
		line-height: 32rpx;
		color: #4b76f9;
		margin-right: 10rpx;
	}
	.characteristic:last-child {
		margin-right: 0;
	}
}

.loading {
	text-align: center;
	font-size: 20upx;
	color: #8f8f8f;
	line-height: 80upx;
}
</style>
